/*****************************************************************************
*    Copyright (C)2003 Ali Corporation. All Rights Reserved.
*
*    File:    nim_m3327.h
*
*    Description:    Header file in LLD.
*    History:
*           Date            Athor        Version          Reason
*	    ============	=============	=========	=================
*	1.	3.11.2004        Hao Huang      Ver 0.1       Create file.
*	2.  8. 9.2005        Berg Xing      Ver 0.2       Merge M227C QPSK into M3327
*	3.  08/31/2005	Zhu Jun		Ver 0.3.		Unify Tuner's configuration API Functions
*
*****************************************************************************/

#ifndef __LLD_NIM_M3327_H__
#define __LLD_NIM_M3327_H__

#include <sys_config.h>
#include <types.h>
#include <retcode.h>
#include <hld/nim/nim_dev.h>
#include <hld/nim/nim_tuner.h>
#include <api/libc/printf.h>

//#define NIM_M3327G_SFU_TEST

//Define M3327G4 SSI clock in KHz.
#define NIM_M3327G_SSI_CLOCK 67500 //54000 //67500 //72000
//Define M3327G4 SSI TS gap.
#define NIM_M3327G_SSI_TS_GAP 0 //0 //4 //8 //16

#define M3327_QPSK_FREQ_OFFSET 1
#define M3327_LOACL_FREQ   5150
#define M3327_DEBUG_FLAG	0
#define QPSK_TUNER_FREQ_OFFSET	4
#define SYS_QPSK_CHIP_ADRRESS		0x20

#define NIM_M3327_BASE_IO_ADR  0xB8003000

#define NIM_GET_DWORD(i)           (*(volatile UINT32 *)(i))
#define NIM_SET_DWORD(i,d)        (*(volatile UINT32 *)(i)) = (d)

#define NIM_GET_WORD(i)             (*(volatile UINT16 *)(i))
#define NIM_SET_WORD(i,d)          (*(volatile UINT16 *)(i)) = (d)

#define NIM_GET_BYTE(i)             (*(volatile UINT8 *)(i))
#define NIM_SET_BYTE(i,d)          (*(volatile UINT8 *)(i)) = (d)

#if(M3327_DEBUG_FLAG)
	#define M3327_DEBUG	PRINTF
	#define NIM_PRINTF 		PRINTF
#else
	#define M3327_DEBUG(...)
	#define NIM_PRINTF(...)
#endif


#define SWITCH_NIM_M3327_DEBUG	0
#define NIM_EXT_DEMOD_RESET	0x00000001

struct nim_m3327_private
{
	struct   QPSK_TUNER_CONFIG_DATA Tuner_Config_Data;
	INT32 	(*nim_Tuner_Init) (UINT32* tuner_id, struct QPSK_TUNER_CONFIG_EXT * ptrTuner_Config);	/* Tuner Initialization Function */
	INT32 	(*nim_Tuner_Control) (UINT32 tuner_id, UINT32 freq, UINT32 sym);	/* Tuner Parameter Configuration Function */
	INT32 	(*nim_Tuner_Status) (UINT32 tuner_id, UINT8 *lock);				/* Get Tuner Status Function */
	UINT32 tuner_id;
	struct nim_device_stats stats;
	UINT8 chip_id;
	UINT8   m_CRNum;
	UINT32  m_CurFreq;
	INT32 (*nim_reg_read)(UINT8, UINT8 *, UINT8);
	INT32 (*nim_reg_write)(UINT8, UINT8 *, UINT8);
	UINT8 is_ext_nim:1;
	UINT8 ext_nim_valid:1;
	UINT8 is_m3327g:1;
	UINT8 reserved5:5;
	UINT8 ext_qpsk_mode;
	UINT8 tsi_mode;
	UINT8 reserved;
	INT32 m_Freq[255];
	UINT32 m_Rs[255];
	INT32	ext_lnb_id;
	INT32 	(*ext_lnb_control) (UINT32, UINT32, UINT32);	

    UINT32 m_SetFreq;
};

INT32 nim_m3327_ioctl(struct nim_device *dev, INT32 cmd, UINT32 param);
INT32 nim_m3327_ioctl_ext(struct nim_device *dev, INT32 event, void* param_list);
INT32 nim_m3327_set_polar(struct nim_device *dev, UINT8 polar);
INT32 nim_m3327_set_12v(struct nim_device *dev, UINT8 flag);
INT32 nim_m3327_channel_search(struct nim_device *dev, UINT32 CRNum);
INT32 nim_m3327_DiSEqC_operate(struct nim_device *dev, UINT32 mode, UINT8* cmd, UINT8 cnt);
INT32 nim_m3327_DiSEqC2X_operate(struct nim_device *dev, UINT32 mode, UINT8* cmd, UINT8 cnt, UINT8 *rt_value, UINT8 *rt_cnt);

INT32 nim_m3327_get_lock(struct nim_device *dev, UINT8 *lock);
INT32 nim_m3327_get_freq(struct nim_device *dev, UINT32 *freq);
INT32 nim_m3327_get_symbol_rate(struct nim_device *dev, UINT32 *sym_rate);
INT32 nim_m3327_get_code_rate(struct nim_device *dev, UINT8* code_rate);

INT32 nim_m3327_get_AGC(struct nim_device *dev, UINT8 *agc);
INT32 nim_m3327_get_SNR(struct nim_device *dev, UINT8 *snr);
INT32 nim_m3327_get_BER(struct nim_device *dev, UINT32 *RsUbc);
INT32 nim_m3327_get_fft_result(struct nim_device *dev, UINT32 freq, UINT32* start_adr );
const char *nim_m3327_get_ver_infor(struct nim_device *dev);
INT32 nim_m3327_FFT( struct nim_device *dev, UINT32 startFreq );
INT32 nim_m3327_AutoScan(struct nim_device *dev, struct NIM_Auto_Scan* pstAuto_Scan);

UINT32 nim_m3327_get_CURFreq(struct nim_device *dev);
UINT8 nim_m3327_get_CRNum(struct nim_device *dev);
void nim_m3327_set_FFT_para(  struct nim_device *dev );
INT32 nim_m3327_reg_get_freq(struct nim_device *dev, UINT32 *freq);
INT32 nim_m3327_reg_get_symbol_rate(struct nim_device *dev, UINT32 *sym_rate);
INT32 nim_m3327_reg_get_code_rate(struct nim_device *dev, UINT8* code_rate);
INT32 nim_m3327_reg_get_tune_freq(struct nim_device *dev, INT32 *freq );
void nim_m3327_clear_int(struct nim_device *dev );

void nim_m3327_set_CodeRate(struct nim_device *dev, UINT8 coderate );
void nim_m3327_set_RS(struct nim_device *dev, UINT32 rs );
void nim_m3327_set_Search_Freq(struct nim_device *dev);
void nim_m3327_set_Search_RS(struct nim_device *dev, UINT32 startrs, UINT32 endrs );
void nim_m3327_set_freq_offset(struct nim_device *dev, INT32 delfreq );

INT32 nim_m3327_soft_search( struct nim_device *dev, UINT32 rs, UINT32 freq, INT32 delfreq );

//static INT32 nim_m3327_tuner_lock(struct nim_device *dev, UINT8 *tun_lock);
//static INT32 nim_m3327_get_FEC_lock(struct nim_device *dev, UINT8 *fec_lock);
UINT8 nim_m3327_get_SNR_index( struct nim_device *dev );
INT32 nim_m3327_fft_find_channel(struct nim_device *dev, UINT32* tune_freq, UINT32 ana_filter_BW );
void nim_m3327_calculate_energy(struct nim_device *dev);
void nim_m3327_fft_result_read(struct nim_device *dev);

#endif	/* __LLD_NIM_M3327_H__ */

